package lzj.tools;

import com.jfinal.kit.StrKit;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Dates {

  private static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

  /**
   * 字符串 "yyyy-MM-dd" 转 Dates 类型
   */
  public static Date str2yyyyMMdd(String str) {
    Date result = null;

    try {
      result = yyyyMMdd.parse(str);
    } catch (Exception e) {
      // [Neo] Empty
      return null;
    }

    return result;
  }

  /**
   * 获取指定格式的当前时间
   *
   * @param pattern 格式
   */
  public static String nowByFormat(String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    String result = null;
    try {
      Date date = new Date();
      result = sdf.format(date);
    } catch (Exception e) {
      // [Neo] Empty
      e.printStackTrace();
    }
    return result;
  }

  /**
   * 获取当前时间，格式为 yyyy-MM-dd-HH-mm-ss
   */
  public static String nowByFormat() {
    return nowByFormat("yyyy-MM-dd-HH-mm-ss");
  }

  /**
   * 获取当前时间，格式为 yyyy-MM-dd HH:mm:ss
   */
  public static String nowNormalTime() {
    return nowByFormat("yyyy-MM-dd HH:mm:ss");
  }

  /**
   * 字符串转时间
   * @param dateStrint
   * @param datetype
   * @return
   */
  public static Date stringToDate(String dateStrint, String datetype) {
    if (!StrKit.notBlank(datetype)) {
      datetype = "yyyy-MM-dd HH:mm:ss";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(datetype);

    Date result = null;

    try {
      result = sdf.parse(dateStrint);
      return result;
    } catch (Exception var3) {
      return null;
    }

  }

  /**
   * 时间转字符串
   * @param date
   * @param pattern
   * @return
   */
  public static String dateToString(Date date ,String pattern) {
    if (!StrKit.notBlank(pattern)){
      pattern ="yyyy-MM-dd";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    String result = null;

    try {
      result = sdf.format(date);
    } catch (Exception var4) {
      var4.printStackTrace();
    }

    return result;
  }

  /**
   * 将长整型的时间毫秒转成时间
   * @param time
   * @return
   */
  public static Date LongToDate(String time){
    Long timestamp = Long.parseLong(time);
    if (time.length()==10){
      timestamp=timestamp*1000;
    }
    return new Date(timestamp);
  }

  /**
   * 获取当前时间
   *
   * @return Date 类型
   */
  public static Date now() {
    return new Date();
  }

  public static Date getDate(String type) {
    Calendar cal = Calendar.getInstance();
    switch (type) {
      case "week":
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        break;
      case "month":
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        break;
      case "quarter":
        int currentMonth = cal.get(Calendar.MONTH) + 1;

        if (currentMonth >= 1 && currentMonth <= 3) {
          cal.set(Calendar.MONTH, 0);
        } else if (currentMonth >= 4 && currentMonth <= 6) {
          cal.set(Calendar.MONTH, 3);
        } else if (currentMonth >= 7 && currentMonth <= 9) {
          cal.set(Calendar.MONTH, 6);
        } else if (currentMonth >= 10 && currentMonth <= 12) {
          cal.set(Calendar.MONTH, 9);
        }
        cal.set(Calendar.DATE, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        break;
      case "year":
        cal.set(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        break;
    }

    return cal.getTime();
  }

}

